﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using FrienDev.BusinessRules;
using FrienDev.Api;
using FrienDev.Data;
using FrienDev.SystemApps;
using System.Collections.Generic;

public partial class Controls_Message_ReceiveMessageBox : System.Web.UI.UserControl
{
    

    protected void Page_Load(object sender, EventArgs e)
    {
        
        this.rpt_ReceiveMessage.DataSource = MessageRules.Instance.GetCurrentUserMessages();
        this.rpt_ReceiveMessage.DataBind();
        if (Session["ReadMessageId"] != null)
        {
            string m_ReadMesssageId = Session["ReadMessageId"].ToString();
            MessageRules.Instance.SignReadMessageByMessageId(m_ReadMesssageId);
            Session.Remove("ReadMessageId");
        }
        
           
    }
    protected void rpt_ReceiveMessage_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Message message = e.Item.DataItem as Message;
        Literal temp_ltShortMessage = e.Item.FindControl("lt_ShortMessage") as Literal;
        temp_ltShortMessage.Text = message.MessageContent;
        HtmlImage temp_cimgPhoto = e.Item.FindControl("cimg_RMPhoto") as HtmlImage;
        temp_cimgPhoto.Attributes["src"] = ApiHelper.Instance.GetUserLogoUrl(message.User.UserId);
        HyperLink temp_hySender = e.Item.FindControl("hy_UserName") as HyperLink;
        temp_hySender.Text = UserRules.Instance.GetUserByUserId(message.SenderId).FullName;
        temp_hySender.NavigateUrl = ApiHelper.Instance.GetUserHomepageLink(message.SenderId);
        Literal temp_ltSendDateTime = e.Item.FindControl("lt_SendTime") as Literal;
        Image temp_imgOnlineLogo = e.Item.FindControl("OnlineLogo") as Image;
        Panel temp_pnReply = e.Item.FindControl("pn_Reply") as Panel;
        Literal temp_ltMessageSessionCount = e.Item.FindControl("lt_MessageCount") as Literal;
        HyperLink temp_hyExpandMessage = e.Item.FindControl("hy_ExpandMessage") as HyperLink;
        HyperLink temp_hyViewMessage = e.Item.FindControl("hy_ViewMessage") as HyperLink;
        HyperLink temp_hyReplyMessage = e.Item.FindControl("hy_ReplyMessage") as HyperLink;
        temp_hyReplyMessage.NavigateUrl = MessageRules.Instance.ViewSendMessageUrl(message.MessageId.ToString());
        temp_hyViewMessage.NavigateUrl = MessageRules.Instance.ViewReceivedMessageUrl(message.MessageId.ToString());
        LinkButton temp_lbtClose = e.Item.FindControl("lbt_Close") as LinkButton;
        temp_lbtClose.CommandArgument = message.MessageId.ToString();
        temp_ltSendDateTime.Text = message.SendDateTime.ToString();
        if (MessageRules.Instance.GetCurrentUserMessageSessionCount(message.MessageId.ToString()) > 1)
        {
            temp_hyExpandMessage.NavigateUrl = MessageRules.Instance.ReplyReceivedMessageUrl(message.MessageId.ToString());
            temp_ltMessageSessionCount.Text = MessageRules.Instance.GetCurrentUserMessageSessionCount(message.MessageId.ToString()).ToString();
            temp_pnReply.Visible = true; 
        }
        

        

    }
    protected void rpt_ReceiveMessage_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            string messageId = e.CommandArgument.ToString();
            MessageRules.Instance.DeleteSessionMessageByMessageId(messageId.ToString());
            this.rpt_ReceiveMessage.DataSource = MessageRules.Instance.GetCurrentUserMessages();
            this.rpt_ReceiveMessage.DataBind();
        }
        
    }

    
}
